﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;

/*
 * ParabolaBullet
 * 轨迹是抛物线的子弹。
 *
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 9/30/2017 9:09:00 AM
 */
namespace TTGameEngine {

public class ParabolaBullet : MagicBall {

    [SerializeField, ReadOnly]
    public ParabolaMoveInfo m_moveInfo;

    public bool isFaceToMoveDir = false;

    Vector3 t_curMoveDir = Vector3.zero;

    public override float GetMoveSpeed() {
        return m_moveInfo.moveSpeed;
    }
    public override void SetMoveSpeed(float moveSpeed) {
        m_moveInfo.moveSpeed = moveSpeed;
    }

    protected override void init(int skillID, uint castID) {
        base.init(skillID, castID);

        if(m_colInfo.m_targetType.IsID((int)TargetSelectType.Self)) {
            targetObjIsSelf = true;
        } else {
            //加载DRCollider组件
            LayerMask layerMask = ObjectManager.Share().GetObjLayerMask(m_colInfo.m_targetType, m_owner.m_objType);
            Transform colBindTF = transform;
            drCollider = GameExt.AddDRColliderComponent(m_colInfo, colBindTF, gameObject, layerMask);
            drCollider.onColliderEnter += OnCollider;
            drCollider.OnHitObject += OnHitEffect;
        }
        if(isFaceToMoveDir) {
            xform.forward = m_owner.objTF.forward;
        }
    }

    public ParabolaMoveInfo MoveInfo {
        get { return m_moveInfo; }
    }

    protected override void Update() {
        float dt = Time.deltaTime;
        bool result = true;
        result &= updateLife(dt);
        //飞行
        if(result) {
            result &= updateMove(dt);
        }
        if(result) {
            result &= updateColliderCount();
        }
        if(isFaceToMoveDir) {
            updateFaceDir();
        }
        if(result == false) {
            OnLifeOver();
        }
    }

    void updateFaceDir() {
        if(t_curMoveDir.IsZero() == false)
            xform.forward = t_curMoveDir;
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="dt"></param>
    /// <returns>true，继续逻辑</returns>
    bool updateMove(float dt) {
        Vector3 nextPos = Vector3.zero;
        bool isMoveEnd = m_moveInfo.ObjMoveStep(dt, xform.position, out nextPos);
        t_curMoveDir = nextPos - xform.position;
        xform.position = nextPos;
        return !isMoveEnd;
    }


    public override void EmitSelf(Vector3 startPos) {
        transform.parent = MagicBallManager.Share().GetRootTF();
        transform.position = startPos;
        base.EmitSelf();
    }

    public override void Fire() {
        base.Fire();
    }
    public override void DeactiveMagicBall(bool isRemove = true) {
        base.DeactiveMagicBall(isRemove);
    }
}

}
